var JWT = require("../util/JWT");
const UserService = require("../services/UserService")

const UserController = {
  login: async (ctx, next) => {
    const { username, password } = ctx.request.body;;
    console.log(username, password);
    const data = await UserService.login(username, password);


    if (data.length === 0) {
      ctx.body = {
        code: 400,
        msg: "用户名或密码错误",
      };
    } else {
      //设置header
      const token = JWT.generate({
        _id: data[0].id,
        username: data[0].username
      }, "10s")

      
      // console.log(token);
      ctx.body = {
        data,
        code: 200,
        msg: "登录成功111",
        token
      };
    }
  },







  addUser: async (ctx, next) => {

  }
};

module.exports = UserController;